<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Hash;
use App\Admin;
use Validator;
use Auth;
use Session;
use Illuminate\Hashing\BcryptHasher;
use Cookie;
use Response;
use Crypt;

class AdminController extends Controller
{
    
    


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

    }

    

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


    //注册首页
    public function getReg()
    {
        return view('admin.register');
    }


    //POST注册
    public function postReg(Request $req)
    {
        // dd($req);
        $validate = Validator::make($req->all() ,
        [
            'adminname'=>'required|min:6|unique:admins',
            'password'=>'required|regex:/\w{6,}/',
        ],
        [
            'adminname.required'=>'用户名必须填写',
            'adminname.min'=>'用户名太短',
            'password.required'=>'密码必须填写',
            'password.regex'=>'密码必须要6位以上,并且要使用字母,数字,下划线',
        ]
        );

        // ->fails()
        if( $validate->fails() ) {
            return back()->withErrors($validate)->withInput();
        }else{

            $data =  Admin::create([
                'adminname' => $req->adminname,
                'createtime' => time(),
                'lastlogin' => time(),
                'password' => bcrypt($req->password),
                'permission' => 1,
            ]);

            // dd($data);

            if($data){
                return redirect('/admin/index')->withCookie(Cookie::make('adminname', $data->adminname, 10));
            }
        }


    }



    //登陆首页
    public function index(Request $req)
    {
        //
        $admins = new Admin();
        $admin = $admins::where('adminname', $req->Cookie('adminname'))->first();
        if ( $admin && ($admin->permission == 1) ) {
            return back();
        }
        //return Crypt::decrypt;
        //验证是否登陆 attempt  BcryptHasher 
        //Auth::user()验证的字段是否是自己想要的表中的数据?
        
        return view('admin.login');
    }


    //用户登陆验证
    public function store(Request $req)
    {
        $admins = new Admin();
        $admin = $admins::where('adminname',$req->adminname)->first();

        //验证用户名字段是否正确
        if (!$admin) {
            return redirect('/admin/login')->withInput()->withErrors('用户名错误!!!');
        }

        $bh = new BcryptHasher;
        if ( !$bh->check($req->password,$admin->password) ) {
            return redirect('/admin/login')->withInput()->withErrors('密码错误!!!');
        }

        if ($admin->permission != 1) {
            return redirect('/admin/login')->withInput()->withErrors('权限不够!!!');
        }

        //验证成功 EncryptCookies
        return redirect('/admin/index')->withCookie(Cookie::make('adminname', $admin->adminname, 10));

    }


    //用户退出
    public function getLogout()
    {
        return redirect('/admin/login')->withCookie(Cookie::make('adminname', '', -1) );
    }
}
